package com.crm.dao.impl;

import com.crm.dao.IProcessDao;
import com.crm.model.Process;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateCallback;
import org.hibernate.Session;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class ProcessDao extends BaseDao implements IProcessDao {

    /**
     * {@inheritDoc}
     */
    public void saveOrUpdate(Process process) {
        getHibernateTemplate().saveOrUpdate(process);
    }

    /**
     * {@inheritDoc}
     */
    public List getProcesses() {
        return getHibernateTemplate().find("from Process");
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessesByContactId(String contactId) {
        return getHibernateTemplate().find("from Process where contactId=?", contactId);
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessesByUserCode(String userCode) {
        return getHibernateTemplate().find("from Process where user.userCode=?", userCode);
    }

    /**
     * {@inheritDoc}
     */
    public com.crm.model.Process getProcessById(String id) {
        List list = getHibernateTemplate().find("from Process where id=?", id);
        return getProcess0(list);
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessesByNoHandle() {
        return getHibernateTemplate().find("from Process where isHandle='0' and remindDate<>'N'");
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessesByNoHandle(String userCode) {
        return getHibernateTemplate().find("from Process where isHandle='0' and user.userCode=? and remindDate<>'N'", userCode);
    }

    /**
     * {@inheritDoc}
     */
    public String getLastSerialNum(String contactId) {
        List list = getHibernateTemplate().find("select max(serialNum) from Process where contactId=?", contactId);
        if (list.isEmpty() || list.get(0) == null) {
            return null;
        } else {
            return (String) list.get(0);
        }
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessesByKeywords(String uc, String customer, String contact, String date) {
        String[] arguments = new String[]{"%" + uc + "%", "%" + customer + "%", "%" + contact + "%", "%" + date + "%"};
        return getHibernateTemplate().find("from Process where user.userCode like ? and customerName like ? and contactId like ? and communeTime like ?", arguments);
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessesByKeywords(String customer, String contact, String date) {
        String[] arguments = new String[]{"%" + customer + "%", "%" + contact + "%", "%" + date + "%"};
        return getHibernateTemplate().find("from Process where customerName like ? and contactId like ? and communeTime like ?", arguments);
    }

    /**
     * {@inheritDoc}
     */
    public void delete(Process process) {
        getHibernateTemplate().delete(process);
    }

    /**
     * {@inheritDoc}
     */
    public int deleteAll(String contactId) {
        final List list = getHibernateTemplate().find("from Process where contactId=?", contactId);
        getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                return session.getNamedQuery("to_his_processes").setParameterList("id", list).executeUpdate();
            }
        });
        getHibernateTemplate().deleteAll(list);
        int i = list.size();
        list.clear();
        return i;
    }

    public List getRecycleProc(final String contactId) {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                return session.getNamedQuery("select_his_processes")
                        .setString("contactId", contactId)
                        .list();
            }
        });
    }

    private Process getProcess0(List list) {
        if (list.isEmpty()) {
            return null;
        } else {
            return (Process) list.get(0);
        }
    }
}
